#include <stdio.h>
#include <stdlib.h>
#include "debugger.h"

int main(int argc, char *argv[]) {
    if (argc < 2) {
        printf("用法: %s <程序路径>\n", argv[0]);
        return 1;
    }
    
    const char *program_path = argv[1];
    
    printf("=== 高级DWARF解析测试 ===\n");
    printf("测试程序: %s\n", program_path);
    
    // 创建调试器实例
    debugger_t *dbg = debugger_create();
    if (!dbg) {
        printf("错误：无法创建调试器实例\n");
        return 1;
    }
    
    printf("1. 测试基本DWARF信息加载...\n");
    if (debugger_load_symbols(dbg, program_path) == 0) {
        printf("✓ 基本DWARF信息加载成功\n");
    } else {
        printf("✗ 基本DWARF信息加载失败\n");
    }
    
    printf("\n2. 测试高级DWARF解析功能...\n");
    if (debugger_load_variable_info_advanced(dbg, program_path) == 0) {
        printf("✓ 高级DWARF解析成功\n");
        printf("   找到 %zu 个变量\n", dbg->dwarf_variables ? dbg->dwarf_variables->count : 0);
    } else {
        printf("✗ 高级DWARF解析失败\n");
    }
    
    printf("\n3. 测试变量类型信息查询...\n");
    if (dbg->dwarf_variables && dbg->dwarf_variables->count > 0) {
        printf("变量列表:\n");
        for (size_t i = 0; i < dbg->dwarf_variables->count; i++) {
            dwarf_variable_info_t *var = &dbg->dwarf_variables->variables[i];
            printf("  %zu. %s (类型: %s, 大小: %zu字节", 
                   i + 1, var->name, var->type_name, var->size);
            if (var->address != 0) {
                printf(", 地址: 0x%lx", var->address);
            }
            if (var->line != 0) {
                printf(", 位置: %s:%u", var->filename, var->line);
            }
            printf(")\n");
        }
    } else {
        printf("未找到变量信息\n");
    }
    
    printf("\n4. 测试特定变量查询...\n");
    const char *test_variables[] = {
        "global_int_var",
        "global_float_var", 
        "global_double_var",
        "global_char_var",
        "global_string_var",
        "global_array_var",
        "global_struct_var",
        NULL
    };
    
    for (int i = 0; test_variables[i] != NULL; i++) {
        dwarf_variable_info_t *var_info = debugger_get_variable_info(dbg, test_variables[i]);
        if (var_info) {
            printf("✓ 找到变量 %s: 类型=%s, 大小=%zu字节\n", 
                   test_variables[i], var_info->type_name, var_info->size);
        } else {
            printf("✗ 未找到变量 %s\n", test_variables[i]);
        }
    }
    
    printf("\n5. 测试重新加载功能...\n");
    if (debugger_reload_variable_info_with_dwarfdump(dbg) == 0) {
        printf("✓ 重新加载成功\n");
    } else {
        printf("✗ 重新加载失败\n");
    }
    
    // 清理资源
    debugger_destroy(dbg);
    
    printf("\n=== 测试完成 ===\n");
    return 0;
}